home *** CD-ROM | disk | FTP | other *** search
/ Aminet 33 / Aminet 33 - October 1999.iso / Aminet / misc / emu / NESDevTools.lha / CHARlie / CHARlie.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-28  |  2.5 KB  |  121 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define CELLSIZE 16
  5.  
  6. int main(int argc,char *argv[])
  7. {
  8.     
  9.     short int dict[CELLSIZE][256];
  10.     short int tempcell[CELLSIZE];
  11.     static int dummy[] = {0x00,0x7E,0x42,0x42,0x42,0x42,0x7E,0x00};
  12.     int maxcell,current,found,match,initial,i,j,getout,taken,given,bytecount;
  13.     FILE *FI,*FO;
  14.     
  15.     fprintf(stderr,"\nCharacter ROM Optimizer (C)1999 Chris Covell (ccovell@direct.ca)\n\n");
  16.  
  17.   if((argc!=3) && (argc!=4))
  18.   {
  19.     fprintf(stderr,"Usage: %s infile outfile [-c]\n\n",argv[0]);
  20.     return(1);
  21.   }
  22.  
  23.   if(!(FI=argc<2? stdin:fopen(argv[1],"rb")))
  24.   { fprintf(stderr,"%s: Can't open input file\n\n",argv[0]);return(1); }
  25.   if(!(FO=argc<2? stdout:fopen(argv[2],"wb")))
  26.   { fprintf(stderr,"%s: Can't open output file\n\n",argv[0]);return(1); }
  27.  
  28.   maxcell=0;
  29.   current=0;
  30.   found=0;
  31.   match=1;
  32.   initial=1;
  33.   getout=0;
  34.   taken=0;
  35.   given=0;
  36.   bytecount=0;
  37.  
  38.  
  39.   while((tempcell[0]=fgetc(FI))>=0)
  40.   {
  41.     taken=taken+1;
  42.     found=0;
  43.     current=0;
  44.     for(i=1; i<CELLSIZE; i=i+1)
  45.     {
  46.         tempcell[i]=fgetc(FI);
  47.         taken=taken+1;
  48.     }
  49.     
  50.     if(initial)
  51.     {
  52.         for(i=0; i<CELLSIZE; i=i+1)
  53.         {
  54.             dict[i][0]=tempcell[i];
  55.             fputc(tempcell[i],FO);
  56.             given=given+1;
  57.             tempcell[i]=fgetc(FI);
  58.             taken=taken+1;
  59.         }
  60.         initial=0;
  61.     }
  62.     
  63.     
  64.     while((found==0) && (current<=maxcell) && (!getout))
  65.     {
  66.         for(i=0; ((i<CELLSIZE) && (match==1)); i=i+1)
  67.         {
  68.             if(tempcell[i] != dict[i][current]) match=0;       
  69.         }
  70.         if(match==1)
  71.         {
  72.             
  73.             if(strncmp(argv[3],"-c",2))
  74.             for(j=0; j<(CELLSIZE / 8); j=j+1)
  75.             for(i=0; i<(CELLSIZE / 2); i=i+1)
  76.             {
  77.                fputc(dummy[i],FO);
  78.                given=given+1;
  79.             }
  80.             found=1;
  81.         }
  82.         current=current+1;
  83.         match=1;
  84.         if(taken>=(CELLSIZE * 256)) getout=1;
  85.     }
  86.     
  87.     if(found==0)
  88.     {
  89.         for(i=0; i<CELLSIZE; i=i+1)
  90.         {
  91.             dict[i][current] = tempcell[i];
  92.             fputc(tempcell[i],FO);
  93.             given=given+1;            
  94.         }
  95.         maxcell=maxcell+1;
  96.     }
  97.     if(getout)
  98.     {
  99.         bytecount=bytecount+taken;
  100.         maxcell=0;
  101.         match=1;
  102.         initial=1;
  103.         current=0;
  104.         found=0;
  105.         getout=0;
  106.         taken=0;
  107.     }
  108.   }
  109.   if(!strncmp(argv[3],"-c",2))
  110.   {
  111.     bytecount=bytecount-given;
  112.     for(i=0; i<bytecount; i=i+1)
  113.     fputc(0x00,FO);
  114.   }
  115.   
  116.    
  117.   fclose(FO);
  118.   fclose(FI);
  119.     
  120.   return(0);
  121. }